#include "Manage.h"

Manage::Manage()
{
    this->pUser = NULL;
    this->shape = NULL;

    //ctor
}

void Manage::InitUser()
{
    cout<<"Please input the filename stored the user message"<<endl;
    string file;
    cin>>file;
    ifstream ifs (file.c_str());
    if (!ifs.is_open())
    {
       cout<<"Open file fail!"<<endl;
       exit(0);
    }
    else
    {
        while(!ifs.eof())
        {
            pUser = new User;
            pUser->Read(&ifs);
            userlist.push_back(pUser);
        }
    }

    int i;
    for (i=0;i<3;i++)
    m_max_mark.push_back(0);
}

bool Manage::LoginSystem()
{
    cout<<"Please input the username"<<endl;
    string UserName;
    cin>>UserName;

    unsigned int i;
    for (i=0;i<userlist.size();i++)
    {
        if (userlist[i]->Islogin(UserName));
        {
            this->pUser = userlist[i];
            cout<<"login succeed"<<endl;
            return true;
        }
    }
    return false;
}

void Manage::ChooseShape()
{
    cout<<"Please select a shape"<<endl;
    cout<<"Alpha --> A"<<endl;
    cout<<"Graph --> G"<<endl;
    cout<<"Number --> N"<<endl;

    cin>>m_shape;
    switch (m_shape)
    {
        case 'A': this->shape = new ShapeAlpha;
                  break;
        case 'N': this->shape = new ShapeNum;
                  break;
        case 'G': this->shape = new ShapeGraph;
                  break;
        default :
                  cout<<"Choose shape failed"<<endl;
    }
    m_score = this->shape->GetScore();
}

void Manage::MakeMaxMark()
{
    if (m_score > m_max_mark[this->shape->GetLevel() -1])
    {
        m_max_mark[this->shape->GetLevel() -1] = m_score;
    }
}

int Manage::GetMaxByLevel(int par_level)
{
    return m_max_mark[par_level-1];
}

void Manage::SelectOption()
{
    cout<<"please choose a selection below"<<endl;
    cout<<"1 --> Print my mark"<<endl;
    cout<<"2 --> Print the best grade of level input"<<endl;
    cout<<"3 --> Change the user"<<endl;
    cout<<"4 --> Replay the game"<<endl;
    cout<<"5 --> Exit the program"<<endl;
    int tmp_level;
    int choosen;
    cin>>choosen;
    switch (choosen)
    {
        case 1: cout<<"Your mark is "<<this->shape->GetScore()<<endl;
                SelectOption();
                break;
        case 2: cout<<"Please input the level"<<endl;
                cin>>tmp_level;
                cout<<"The best grade of "<<tmp_level<<" is "<<GetMaxByLevel(tmp_level)<<endl;
                SelectOption();
                break;
        case 3: if (LoginSystem())
                  Run();
                break;
        case 4: Run();
                break;
        case 5: cout<<"The progress is over"<<endl;
                sleep(1);
                exit (0);
    }
}

void Manage::Run()
{
    ChooseShape();
    this->shape->PlayGame();
    this->pUser->SaveToRecord(m_score , this->shape->GetLevel() , m_shape);
    SelectOption();
}

Manage::~Manage()
{
    //dtor
}


